//
// Created by Administrator on 2023/2/10.
//

#ifndef SW_V325_DRV_LCD_ST77903_H
#define SW_V325_DRV_LCD_ST77903_H

#include "../../main/usercfg.h"
#define INIT_DAT_LEN (30)

// #define BIST_MODE

typedef struct {
  uint8_t cmd;
  uint8_t len;
  uint8_t dat[INIT_DAT_LEN];
} init_line_t;

/* ST77903 LCD Initialize table */
/* command - parameter length - parameter data */
/* if command is 0xff, means a delay time unit millisecond */
static const init_line_t init_table[] = {
    {0xf0, 1, {0xc3}},
    {0xf0, 1, {0x96}},
    {0xf0, 1, {0xa5}},
    {0xe9, 1, {0x20}},
    {0xe7, 4, {0x80, 0x77, 0x1f, 0xcc}},
    {0xc1, 4, {0x77, 0x07, 0xc2, 0x07}},
    {0xc2, 4, {0x77, 0x07, 0xc2, 0x07}},
    {0xc3, 4, {0x22, 0x02, 0x22, 0x04}},
    {0xc4, 4, {0x22, 0x02, 0x22, 0x04}},
    {0xc5, 1, {0x71}},
    {0xe0, 14,{0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b, 0x17,0x2a, 0x2f}},
    {0xe1, 14,{0x87, 0x09, 0x0c, 0x06, 0x05, 0x03, 0x29, 0x32, 0x49, 0x0f, 0x1b, 0x17,      0x2a, 0x2f}},
    {0xe5, 14,{0xb2, 0xf5, 0xbd, 0x24, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22,      0x22, 0x22}},
    {0xe6, 14,{0xb2, 0xf5, 0xbd, 0x24, 0x22, 0x25, 0x10, 0x22, 0x22, 0x22, 0x22, 0x22,      0x22, 0x22}},
    {0xec, 2, {0x40, 0x03}},
    {0x36, 1, {0x0c}},
    {0x3a, 1, {0x05}}, // RGB显示格式初始化 0101：565 / 110 ：666 / 111：888
    {0xb2, 1, {0x00}},
    {0xb3, 1, {0x01}},
    {0xb4, 1, {0x00}},
    {0xb5, 4, {0x00, 0x08, 0x00, 0x08}},
    {0xb6, 2, {0xc7, 0x31}},
    {0xa5, 9, {0x00, 0x00, 0x00, 0x00, 0x20, 0x15, 0x2a, 0x8a, 0x02}},
    {0xa6, 9, {0x00, 0x00, 0x00, 0x00, 0x20, 0x15, 0x2a, 0x8a, 0x02}},
    {0xba, 7, {0x0a, 0x5a, 0x23, 0x10, 0x25, 0x02, 0x00}},
    {0xbb, 8, {0x00, 0x30, 0x00, 0x29, 0x88, 0x87, 0x18, 0x00}},
    {0xbc, 8, {0x00, 0x30, 0x00, 0x29, 0x88, 0x87, 0x18, 0x00}},
    {0xbd, 11,{0xa1, 0xb2, 0x2b, 0x1a, 0x56, 0x43, 0x34, 0x65, 0xff, 0xff, 0x0f}},
    {0x35, 1, {0x00}},
    {0x21, 1, {0x00}},
    {0x11, 1, {0x00}},
    {0xff, 1, {120}},
    {0x29, 1, {0x00}}, // DISPON-0X29 /DISPOFF-0X28
    {0xff, 1, {120}},
#ifdef BIST_MODE
    {0xb0, 1, {0xa5}},
    {0xcc, 9, {0x40, 0x00, 0x3f, 0x00, 0x14, 0x14, 0x20, 0x20, 0x03}},
#endif
};

#define LCD_BPP 16 // RGB565

#define LCD_X_SIZE (LV_HOR_RES_MAX) /* available x pixel size */
#define LCD_Y_SIZE (LV_VER_RES_MAX) /* available y pixle size */

#define LCD_PBYTE (LCD_BPP / 8)            /* bytes in pixel unit */
#define LCD_HBYTE (LCD_X_SIZE * LCD_PBYTE) /* bytes in horizontal line */

#define LCD_VSW 1U
#define LCD_HFP 8U
#define LCD_HBP 8U
//
extern EXT_RAM_BSS_ATTR uint8_t frame_cache[LCD_Y_SIZE][LCD_HBYTE];

void lcd_qspi_bus_init(void);
void lcd_initialize(void);
void lcdqspi_clear(uint32_t color);
void lcd_transmit(uint8_t cmd, int len, const uint8_t *data);
void lcdqspi_set_pixel_rgb565(uint32_t x, uint32_t y, uint16_t color);
void lcdqspi_draw_line_rgb565(uint32_t x0, uint32_t x1, uint32_t y,
                              uint16_t *pixel);

#endif // SW_V325_DRV_LCD_ST77903_H
